package com.example.talksdk;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import androidx.appcompat.widget.Toolbar;
import UserAdapter.FriendsAdapter;
import UserDecoration.SpacesItemDecoration;
import UserGlobal.GlobalData;
import UserThread.ChatMesRecieveThread;
import UserThread.DeleteThread;
import UserThread.FriendSwipeThread;
public class PersonalHome extends AppCompatActivity {
    FriendsAdapter mFriendsAdapter;
    LinearLayoutManager mLinearLayoutManager;
    ChatMesRecieveThread thread;
    RecyclerView mRecyclerView;
    Runnable mAction;//recycleview更新行为
    Boolean isSkip=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.personal_home);
        mRecyclerView = findViewById(R.id.friendslist);
        mRecyclerView.addItemDecoration(new SpacesItemDecoration(30));
        Toolbar mToolbar = findViewById(R.id.personalhometoolbar);
        setSupportActionBar(mToolbar);
        mFriendsAdapter = new FriendsAdapter();
        mAction=new Runnable() {
            @Override
            public void run() {
                mRecyclerView.setLayoutManager(mLinearLayoutManager);
                mRecyclerView.setAdapter(mFriendsAdapter);
            }
        };
        mLinearLayoutManager = new LinearLayoutManager(PersonalHome.this, RecyclerView.VERTICAL, false);
        mToolbar.setTitle("hello "+GlobalData.Myid);
        new FriendSwipeThread(mAction,PersonalHome.this).start();//请求好友列表
        thread=new ChatMesRecieveThread(null,null);
        thread.start();
        mFriendsAdapter.setmItemOnClickListener(new FriendsAdapter.ItemOnClickListener() {
            @Override
            public void onclick(FriendsAdapter.ViewHolder view, int postion) {
                GlobalData.Friendid=view.mTextView.getText().toString();
                isSkip=true;
                Intent intent=new Intent(PersonalHome.this,ChatUi.class);
                startActivity(intent);
            }
        });
    }
    @Override
    protected void onStart() {
        if (GlobalData.Myid.equals("")){
            finish();
        }
        super.onStart();
    }
    @Override
    protected void onStop() {
        if(!isSkip){
            new DeleteThread(GlobalData.Myid).start();
            finish();
        }
        super.onStop();
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.toolbarmenu,menu);
        return super.onCreateOptionsMenu(menu);
    }
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        //刷新功能
        if(item.getItemId()==R.id.swipe){
            mRecyclerView.removeAllViewsInLayout();
            new FriendSwipeThread(mAction,PersonalHome.this).start();
        }else if(item.getItemId()==android.R.id.home){
            //new DeleteThread(GlobalData.Myid).start();
        }
        return super.onOptionsItemSelected(item);
    }
    //监听系统返回
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode==KeyEvent.KEYCODE_BACK){
            new DeleteThread(GlobalData.Myid).start();
        }
        return super.onKeyDown(keyCode, event);
    }
}